package com.xthink.task;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xthink.common.utils.HttpClientUtil;
import com.xthink.common.utils.TranSidUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
@Configuration
@Slf4j
public class Task {
    private final static ObjectMapper mapper = new ObjectMapper();

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    //每50分钟获取移动TOKEN
    @Scheduled(cron = "*/2 * * * * ?")
    public void getToken(){

        log.info("定时任务开始了...");


        /*HttpRequest request = HttpRequest.get("https://api.iot.10086.cn/v5/ec/get/token?appid=C5010100070967100100000&password=H4XUbBqpW,AF&transid=" + TranSidUtils.getTranSid());
        HttpResponse response = request.execute();
        String body = response.body();
        try {
            JsonNode jsonNode = mapper.readTree(body);
            if( ObjectUtils.isNull(jsonNode.get("result"))){
                // 这里就是没有token  就可以输出那边给的响应码和错误信息
                Integer status = jsonNode.get("status").asInt();
                System.out.println(status);
                String message = jsonNode.get("message").asText();
                System.out.println(message);
            }else {
                String token = jsonNode.get("result").get(0).get("token").asText();
                System.out.println(token);
                redisTemplate.opsForValue().set("token",token,50, TimeUnit.SECONDS);
            }
        } catch (JsonProcessingException e) {
            log.error(String.format("获取结果失败：%s",e.getMessage()));
            e.printStackTrace();
        }*/


    }
}
